﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eshouse.Bll;
using Eshouse.Entity;
using System.Text;

namespace Eshouse.web.Admin
{
    public partial class CatagoryMan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindCatagoryList();
            }
        }
        private void BindCatagoryList()
        {
            catagoryAccess ca = new catagoryAccess();
            IList<DicCode> list = ca.GetCatagoryList();
            ListItem item;
            DicCode dc;
            if (list == null)
            {
                 item= new ListItem("读取类型失败!", "");
                 ddlCatagory.Items.Add(item);
            }
            else {
                for (int i = 0; i < list.Count;i++ )
                {
                    dc=list[i];
                    item = new ListItem(dc.DictName,dc.Id.ToString());
                    ddlCatagory.Items.Add(item);
                }
            }
        }

        protected void addCatagory_Click(object sender, EventArgs e)
        {
            string catagoryName = catagoryTxt.Text;
            StringBuilder sb=new StringBuilder();
            catagoryTxt.Focus();
            //执行以后服务器验证
            if (catagoryAccess.VerifyCatagoryNameOnServer(catagoryName,ref sb))
            {
                DicCode newCatagory = new DicCode(catagoryName);
                catagoryAccess ca = new catagoryAccess();
                if (!ca.InsertCatagroy(newCatagory))
                {
                   
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script language='javascript'>alert('对不起！发生未知意外,添加失败！请您稍后再试，或者联系管理员。')</script>");
                }
            }
            else{
                    Page.ClientScript.RegisterStartupScript(this.GetType(),"msg","<script language='javascript'>alert('"+sb.ToString()+"')</script>");
            }
        }
    }
}

